//
//  175_计算二叉树的深度、高度.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/2/6.
//

import UIKit

class Solution1 {
    //    获取树的高度(递归)
    func getTreeHeight(_ node: TreeNode<Int>?) -> Int {
        guard let node = node else {
            return 0
        }
        return 1 + max(getTreeHeight(node.left), getTreeHeight(node.right))
    }
    //    迭代（层序遍历）
    func getTreeHeight1(_ node: TreeNode<Int>?) -> Int {
        guard let node = node else {
            return 0
        }
        var height = 0
        var levelSize = 1
        var queue = [TreeNode<Int>]()
        queue.append(node)
        while queue.count > 0 {
            let item = queue.removeFirst()
            levelSize -= 1
            if let left = item.left {
                queue.append(left)
            }
            if let right = item.right {
                queue.append(right)
            }
            if levelSize == 0 {
                levelSize = queue.count
                height += 1
            }
        }
        return height
    }

}
